#include <func.h>
#include"train.h"
void recv_file(int sfd, char *filename)
{
    train_t train;
    memset(&train, 0, sizeof(train));
    printf("start recv_file\n");
    printf("---filename is %s\n",filename);

    recvn(sfd, &train.length, 4);
    printf("train.len=%d\n", train.length);
    if (train.length == -1)
    {
        printf("--server not have: %s--\n",filename);
        return;

    }
    memset(&train, 0, sizeof(train));
    int fd = open(filename, O_CREAT | O_RDWR | O_APPEND, 0666);
    if (fd == -1)
    {
        printf("open error\n");
        return;

    }
    memset(&train, 0, sizeof(train));
    off_t filesize;
    recvn(sfd, &train.length, sizeof(train.length));
    recvn(sfd, &filesize, train.length);
    int left = filesize, cur = 0, ret = 0;
    while (left > 0)
    {
        memset(&train, 0, sizeof(train));
        recvn(sfd, &train.length, 4);
        recvn(sfd, train.data, train.length);
        ret = write(fd, train.data, train.length);
        if (ret == -1)
        {
            printf("write error\n");
            return;

        }
        cur += ret;
        left -= ret;

    }
    printf("recv file success\n");

}




